<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 商品接口控制器
 */
class Goods extends ApiBase
{
	/**
	 * 商品预览
	 */
	public function goodsInfos()
	{
		return $this->apiReturn($this->logicGoods->goodsInfos($this->param));
	}
	/**
	 * 二维码
	 */
	public function erweima()
	{
		return $this->apiReturn($this->logicGoods->erweima($this->param));
	}
	/**
	 * 微信分享
	 */
	public function wxShare()
	{
		return $this->apiReturn($this->logicGoods->wxShare($this->param));
	}
	/**
	 * 获取地址
	 */
	public function userOrderInfo()
	{
		return $this->apiReturn($this->logicGoods->userOrderInfo($this->param));
	}
	/**
	 * 优惠券列表
	 */
	public function newsList()
	{
		$list=parse_config_array('newsConfig');
		return $this->apiReturn($list);
	}
	/**
	 * 优惠券列表
	 */
	public function serviceOrderList()
	{
		return $this->apiReturn($this->logicGoods->serviceOrderList($this->param));
	}
	/**
	 * 查看核销码
	 */
	public function orderCode()
	{
		return $this->apiReturn($this->logicGoods->orderCode($this->param));
	}
	/**
	 * 消费券使用
	 */
	public function opOrder()
	{
		return $this->apiReturn($this->logicGoods->opOrder($this->param));
	}
	/**
	 * 优惠券列表
	 */
	public function yhqList()
	{
		return $this->apiReturn($this->logicGoods->yhqList($this->param));
	}
	/**
	 * 优惠券列表
	 */
	public function serviceOrderLists()
	{
		return $this->apiReturn($this->logicGoods->serviceOrderLists($this->param));
	}
	/**
	 * 确认收货
	 */
	public function qrsh()
	{
		return $this->apiReturn($this->logicGoods->qrsh($this->param));
	}
	/**
	 * 分类页面
	 */
	public function goodsCatLists()
	{
		return $this->apiReturn($this->logicGoods->goodsCatLists($this->param));
	}
	/**
	 * 抢购专区
	 */
	public function goodsLists()
	{
		return $this->apiReturn($this->logicGoods->goodsLists($this->param));
	}
	/**
	 * 线下商家
	 */
	public function dianGoodsList()
	{
		return $this->apiReturn($this->logicGoods->dianGoodsList($this->param));
	}
	/**
	 * 订单添加
	 */
	public function orderAdd()
	{
		return $this->apiReturn($this->logicGoods->orderAdd($this->param));
	}
	/**
	 * 线下订单列表
	 */
	public function userOrderList()
	{
		return $this->apiReturn($this->logicGoods->userOrderList($this->param));
	}
	/**
	 * 地址列表
	 */
	public function addressList()
	{
		return $this->apiReturn($this->logicGoods->getAddressList($this->param));
	}
	/**
	 * 地址添加、编辑
	 */
	public function addressEdit()
	{
		return $this->apiReturn($this->logicGoods->addressEdit($this->param));
	}
	/**
	 * 地址删除
	 */
	public function addressDel()
	{
		return $this->apiReturn($this->logicGoods->addressDel($this->param));
	}
	/**
	 * 首页
	 */
	public function dianList()
	{
		return $this->apiReturn($this->logicGoods->dianList($this->param));
	}
	/**
	 * 首页
	 */
	public function goodsIndex()
	{
		return $this->apiReturn($this->logicGoods->goodsIndex($this->param));
	}

	 /**
     * 获取地址
     */
    public function userAddress()
    {        	
        return $this->apiReturn($this->logicGoods->userAddress($this->param));
    }
	 /**
     * 门店详情
     */
    public function dianInfo()
    {        	
        return $this->apiReturn($this->logicGoods->dianInfo($this->param));
    }
	 /**
     * 订单列表
     */
    public function orderList()
    {        	
        return $this->apiReturn($this->logicGoods->orderList($this->param));
    }
	/**
	 * 订单详情
	 */
	public function orderInfo()
	{
		return $this->apiReturn($this->logicGoods->orderInfo($this->param));
	}
	 /**
     * 购买商品
     */
    public function cartOrder()
    {        	
        return $this->apiReturn($this->logicGoods->cartOrder($this->param));
    }
	 /**
     * 购物车列表
     */
    public function goodsCartList()
    {        	
        return $this->apiReturn($this->logicGoods->goodsCartList($this->param));
    }
	 /**
     * 加入购物车
     */
    public function goods_cart()
    {        	
        return $this->apiReturn($this->logicGoods->goodsCart($this->param));
    }
	 /**
     * 删除购物车商品
     */
    public function goodsCartDel()
    {        	
        return $this->apiReturn($this->logicGoods->goodsCartDel($this->param));
    }

     /**
     * 商品详情
     */
    public function goodsInfo()
    {        	
        return $this->apiReturn($this->logicGoods->goodsInfo($this->param));
    }
	 /**
     * 商品列表
     */
    public function goodsList()
    {        	
        return $this->apiReturn($this->logicGoods->goodsList($this->param));
    }
	 /**
     * 用户资料
     */
    public function userAttestation()
    {   
		$data=$this->param;
		$info=db('attestation')->where('user_id',$data['user_id'])->find();
		if(!empty($info)){
			if(!empty($info['bank'])){
				$info['banklist']=explode(',',substr($info['bank'],0,-1));
			}
			if(!empty($info['drive'])){
				$info['drivelist']=explode(',',substr($info['drive'],0,-1));
			}
			if(!empty($info['zx'])){
				$info['zxlist']=explode(',',substr($info['zx'],0,-1));
			}
			if(!empty($info['ls'])){
				$info['lslist']=explode(',',substr($info['ls'],0,-1));
			}
			if(!empty($info['add'])){
				$info['addlist']=explode(',',substr($info['add'],0,-1));
			}
			return $this->apiReturn($info);
		}else{
			return $this->apiReturn(null);
		}
		
    }
	 /**
     * 资料提交
     */
    public function attestationAdd()
    {        	
        $data=$this->param;
		$arr=['user_id'=>$data['user_id'],'gid'=>$data['gid'],'create_time'=>time()];
        $card_front = request()->file('card_front');        
        if($card_front){
            $info = $card_front->validate(['size'=>1024*1024*1,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
            if($info){
                $arr['card_front']='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info->getFilename();			
            }else{
                // 上传失败获取错误信息
                return $this->apiReturn(-[API_CODE_NAME => 1020001, API_MSG_NAME => $card_front->getError()]);
            }
        }
        $card_back = request()->file('card_back');        
        if($card_back){
            $info1 = $card_back->validate(['size'=>1024*1024*1,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
            if($info1){
                $arr['card_back']='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info1->getFilename();		
            }else{
                // 上传失败获取错误信息
                return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $card_back->getError()]);
            }
        }
		$files3 = request()->file('bank');
		$bank='';
		if($files3){
			foreach($files3 as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info7 = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
				if($info7){
					$bank.='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info7->getFilename().',';	
				}else{
					// 上传失败获取错误信息
					return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
				}    
			}
			$arr['bank']=$bank;
		}
		$files4 = request()->file('drive');
		$drive='';
		if($files4){
			foreach($files4 as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info8 = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
				if($info8){
					$drive.='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info8->getFilename().',';	
				}else{
					// 上传失败获取错误信息
					return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
				}    
			}
			$arr['drive']=$drive;
		}
		
		$files = request()->file('zx');
		$zx='';
		if($files){
			foreach($files as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info4 = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
				if($info4){
					$zx.='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info4->getFilename().',';	
				}else{
					// 上传失败获取错误信息
					return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
				}    
			}
			$arr['zx']=$zx;
		}
		$files1 = request()->file('ls');
		$ls='';
		if($files1){
			foreach($files1 as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info5 = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
				if($info5){
					$ls.='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info5->getFilename().',';	
				}else{
					// 上传失败获取错误信息
					return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
				}    
			}
			$arr['ls']=$ls;
		}
		$files2 = request()->file('add');
		$add='';
		if($files2){
			foreach($files2 as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info6 = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move(ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'user' . DS .$data['user_id']);
				if($info6){
					$add.='/upload/extend/user/'.$data['user_id'].'/'.date('Ymd').'/'.$info6->getFilename().',';	
				}else{
					// 上传失败获取错误信息
					return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
				}    
			}
			$arr['add']=$add;
		}
		$is=db('attestation')->where('user_id',$data['user_id'])->value('id');
		if(!empty($is)){
			$res=db('attestation')->where('user_id',$data['user_id'])->update($arr);
		}else{
			$res=db('attestation')->insert($arr);
		}
		if($res != false){
			return $this->apiReturn();
		}else{
			return $this->apiReturn(['code' => 1020001, API_MSG_NAME => '上传失败']);
		}
		
    }
}
